package com.woniuxy.micromallserver.commoditymag.mapper;

import com.woniuxy.micromallserver.bean.Commodity;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @Author Chan
 * @Date 2021/1/21 17:27
 */
@Mapper
public interface CommodityMapper {

    /**
     * 分页查询所有商品
     * @return
     * */
    @Select(value = {" <script>" +
            " SELECT * FROM commodity " +
            " <where> 1=1 " +
            " <if test=\"commodity.commodityName != null and commodity.commodityName != ''\"> AND commodity_name like concat('%', #{commodity.commodityName}, '%') </if> " +
            " <if test=\"commodity.commodityCode != null and commodity.commodityCode != ''\"> and commodity_code like concat('%', #{commodity.commodityCode}, '%') </if> " +
            " <if test=\"commodity.commodityCategory != null and commodity.commodityCategory != ''\"> and commodity_category = #{commodity.commodityCategory}</if> " +
            " <if test=\"commodity.commodityStatus != null and commodity.commodityStatus != ''\"> and commodity_status = #{commodity.commodityStatus}</if> " +
            " </where>" +
            " </script>"})
    @Results({
            @Result(property = "commodityId", column = "commodity_id"),
            @Result(property = "commodityName", column = "commodity_name"),
            @Result(property = "commodityCategory", column = "commodity_category"),
            @Result(property = "commodityPicture", column = "commodity_picture"),
            @Result(property = "commodityTitle", column = "commodity_title"),
            @Result(property = "commodityCode", column = "commodity_code"),
            @Result(property = "commodityPrice", column = "commodity_price"),
            @Result(property = "commodityStock", column = "commodity_stock"),
            @Result(property = "commodityStatus", column = "commodity_status"),
            @Result(property = "commodityDescription", column = "commodity_description")
    })
    List<Commodity> getCommoditiesByPage(@Param("commodity") Commodity commodity);

    /**
     * 查询所有商品
     * @return
     * */
    @Select("select * from commodity")
    @Results({
            @Result(property = "commodityId", column = "commodity_id"),
            @Result(property = "commodityName", column = "commodity_name"),
            @Result(property = "commodityCategory", column = "commodity_category"),
            @Result(property = "commodityPicture", column = "commodity_picture"),
            @Result(property = "commodityTitle", column = "commodity_title"),
            @Result(property = "commodityCode", column = "commodity_code"),
            @Result(property = "commodityPrice", column = "commodity_price"),
            @Result(property = "commodityStock", column = "commodity_stock"),
            @Result(property = "commodityStatus", column = "commodity_status"),
            @Result(property = "commodityDescription", column = "commodity_description")
    })
    List<Commodity> getAllCommodities();

    /**
     * 根据商品状态查询商品
     * @param commodityStatus
     * @return
     * */
    @Select("select * from commodity where commodity_status = #{commodityStatus}")
    @Results({
            @Result(property = "commodityId", column = "commodity_id"),
            @Result(property = "commodityName", column = "commodity_name"),
            @Result(property = "commodityCategory", column = "commodity_category"),
            @Result(property = "commodityPicture", column = "commodity_picture"),
            @Result(property = "commodityTitle", column = "commodity_title"),
            @Result(property = "commodityCode", column = "commodity_code"),
            @Result(property = "commodityPrice", column = "commodity_price"),
            @Result(property = "commodityStock", column = "commodity_stock"),
            @Result(property = "commodityStatus", column = "commodity_status"),
            @Result(property = "commodityDescription", column = "commodity_description")
    })
    List<Commodity> getCommoditiesByStatus(@Param("commodityStatus") int commodityStatus);

    /**
     * 根据商品id查询单个商品
     * @param commodityId
     * @return
     * */
    @ResultType(Commodity.class)
    @Select("select * from commodity where commodity_id = #{commodityId}")
    @Results({
            @Result(property = "commodityId", column = "commodity_id"),
            @Result(property = "commodityName", column = "commodity_name"),
            @Result(property = "commodityCategory", column = "commodity_category"),
            @Result(property = "commodityPicture", column = "commodity_picture"),
            @Result(property = "commodityTitle", column = "commodity_title"),
            @Result(property = "commodityCode", column = "commodity_code"),
            @Result(property = "commodityPrice", column = "commodity_price"),
            @Result(property = "commodityStock", column = "commodity_stock"),
            @Result(property = "commodityStatus", column = "commodity_status"),
            @Result(property = "commodityDescription", column = "commodity_description")
    })
    Commodity getCommodityById(@Param("commodityId") int commodityId);

    /**
     * 多条件查询商品
     * @param commodity
     * @return
     * */
    @ResultType(Commodity.class)
    @Select(value = {" <script>" +
            " SELECT * FROM commodity " +
            " <where> 1=1 " +
            " <if test=\"commodity.commodityName != null\"> and commodity_name like concat('%', #{commodity.commodityName}, '%')</if> " +
            " <if test=\"commodity.commodityCode != null\" >  and commodity_code=#{commodity.commodityCode}</if> " +
            " <if test=\"commodity.commodityCategory != null\" >  and commodity_category=#{commodity.commodityCategory}</if> " +
            " <if test=\"commodity.commodityStatus != null\" >  and commodity_status=#{commodity.commodityStatus}</if> " +
            " </where>" +
            " </script>"})
    @Results({
            @Result(property = "commodityId", column = "commodity_id"),
            @Result(property = "commodityName", column = "commodity_name"),
            @Result(property = "commodityCategory", column = "commodity_category"),
            @Result(property = "commodityPicture", column = "commodity_picture"),
            @Result(property = "commodityTitle", column = "commodity_title"),
            @Result(property = "commodityCode", column = "commodity_code"),
            @Result(property = "commodityPrice", column = "commodity_price"),
            @Result(property = "commodityStock", column = "commodity_stock"),
            @Result(property = "commodityStatus", column = "commodity_status"),
            @Result(property = "commodityDescription", column = "commodity_description")
    })
    List<Commodity> getCommoditiesByMultiConditions(@Param("commodity") Commodity commodity);

    /**
     * 新增商品
     * @param commodity
     * @return int
     * */
    @Insert("insert into commodity values(#{commodity.commodityId},#{commodity.commodityName},#{commodity.commodityCategory},#{commodity.commodityPicture},#{commodity.commodityTitle},#{commodity.commodityCode}," +
            "#{commodity.commodityPrice},#{commodity.commodityStock},#{commodity.commodityStatus},#{commodity.commodityDescription})")
    int addCommodity(@Param("commodity") Commodity commodity);

    /**
     * 根据商品id上架、下架、删除商品，分别改变commodity_status为1、2、5
     * @param commodityId
     * @return int
     * */
    @Update("update commodity set commodity_status = #{commodityStatus} where commodity_id = #{commodityId}")
    int changeCommodityStatus(@Param("commodityId") int commodityId, @Param("commodityStatus") int commodityStatus);

    /**
     * 更新商品信息
     * @param
     * @return
     * */
    @Update(value = {"<script>" +
            "     update commodity  \n" +
            "     <set>  \n" +
            "       <if test=\"commodity.commodityName != null\">  \n" +
            "         commodity_name = #{commodity.commodityName},  \n" +
            "       </if>  \n" +
            "       <if test=\"commodity.commodityCategory != null\">  \n" +
            "         commodity_category = #{commodity.commodityCategory},  \n" +
            "      </if>  \n" +
            "       <if test=\"commodity.commodityPicture != null\">  \n" +
            "         commodity_picture = #{commodity.commodityPicture},  \n" +
            "      </if>  \n" +
            "       <if test=\"commodity.commodityTitle != null\">  \n" +
            "         commodity_title = #{commodity.commodityTitle},  \n" +
            "      </if>  \n" +
            "       <if test=\"commodity.commodityCode != null\">  \n" +
            "         commodity_code = #{commodity.commodityCode},  \n" +
            "      </if>  \n" +
            "       <if test=\"commodity.commodityPrice != null\">  \n" +
            "         commodity_price = #{commodity.commodityPrice},  \n" +
            "      </if>  \n" +
            "       <if test=\"commodity.commodityStock != null\">  \n" +
            "         commodity_stock = #{commodity.commodityStock},  \n" +
            "      </if>  \n" +
            "       <if test=\"commodity.commodityDescription != null\">  \n" +
            "         commodity_description = #{commodity.commodityDescription},  \n" +
            "      </if>  \n" +
            "     </set> " +
            "     where commodity_id = #{commodity.commodityId}" +
            "      </script>"
    })
    int updateCommodity(@Param("commodity") Commodity commodity);

}
